package cn.itlight.system.controller;

import cn.itlight.basic.annotation.PreAuthorize;
import cn.itlight.basic.utils.AjaxResult;
import cn.itlight.basic.utils.PageList;

import cn.itlight.system.domain.Dictionary;
import cn.itlight.system.query.DictionaryQuery;
import cn.itlight.system.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionary")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DictionaryController {

    @Autowired
    private IDictionaryService dictionaryService;

    /*
     * @Title: loadAll
     * @Description: 查询所有
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/6/24 10:07
     * @Parameters:
     * @Return
     */
    @GetMapping
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(name = "列表字典", sn = "dictionary:list")
    public List<Dictionary> loadAll(){
        return dictionaryService.loadAll();
    }

    /*
     * @Title: loadById
     * @Description: 根据id查询
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/6/24 10:07
     * @Parameters:
     * @Return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "查询字典", sn = "dictionary:get")
    public Dictionary loadById(@ApiParam(value="ID属性",required = true) @PathVariable Long id){
        return dictionaryService.loadById(id);
    }

    /*
     * @Title: remove
     * @Description: 根据id删除
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/6/24 10:59
     * @Parameters:
     * @Return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除字典", sn = "dictionary:delete")
    public AjaxResult remove(@PathVariable Long id){
        try {
            dictionaryService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

    /*
     * @Title: addOrUpt
     * @Description: 新增/修改
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/6/24 11:03
     * @Parameters:
     * @Return
     */
    @PutMapping
    @PreAuthorize(name = "新增或修改字典", sn = "dictionary:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Dictionary dictionary){
        try {
            if(dictionary.getId() == null){
                dictionaryService.save(dictionary);
            } else{
                dictionaryService.update(dictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }

    /*
     * @Title: addOrUpt
     * @Description: 高级查询/分页
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/6/24 11:05
     * @Parameters:
     * @Return
     */
    @PostMapping
    @PreAuthorize(name = "高级查询字典", sn = "dictionary:listQuery")
    public PageList queryData(@RequestBody DictionaryQuery dictionary){
        return dictionaryService.queryData(dictionary);
    }

    /*
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: TomLiu
     * @Version: 1.0
     * @Date:  2022/6/25 18:02
     * @Parameters:
     * @Return
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除字典", sn = "dictionary:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            dictionaryService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试！");
        }
    }
}
